package com.stephenone.customviewdemo

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import kotlin.math.cos
import kotlin.math.sin

/**
 * Created by wenzhubin on 2020/4/28.
 */
class PieCharView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val RADIUS = 300f
    private val ANGELS = floatArrayOf(60f,120f,90f,90f)
    private val COLORS = listOf(Color.CYAN,Color.DKGRAY,Color.MAGENTA,Color.YELLOW)
    private val OFFSET = 50f
    init {

    }
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (canvas != null) {
            var startAngel = 0f
            for((index,angel) in ANGELS.withIndex()){
                if (index == 2) {
                    canvas.save()
                    canvas.translate(cos(Math.toRadians(startAngel + angel / 2.toDouble()).toFloat()) * OFFSET, sin(Math.toRadians(startAngel + angel / 2.toDouble()).toFloat()) * OFFSET)
                }
                paint.setColor(COLORS[index])
                canvas.drawArc(width / 2 - RADIUS,height / 2 - RADIUS,width / 2 + RADIUS,
                    height / 2 + RADIUS,startAngel,angel,true,paint)
                startAngel += angel

                if (index == 2){
                    canvas.restore()
                }
            }
        }
    }
}